package org.link.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.link.service.ArticleService;
import org.link.service.BoardService;
import org.link.service.UserService;


/**
 * 
 * project_name:forumweb
 * package_name:org.link.web
 * file_name:UserIndexServlet.java
 * @author Link
 * create_date:2015年10月25日下午10:33:31
 * modified_date:
 * description: 用户登录主界面
 *
 */
public class UserIndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserService userService = new UserService();
	private BoardService boardService = new BoardService();
	private ArticleService articleService = new ArticleService();

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		int rows = 4;		//显示行数
		int beginIndex = 0;//起始显示索引位置
		int totalPage = 0;//总页数
		int allMainArticCount = articleService.articleCount(0);//主贴子总数
		if(allMainArticCount%rows==0){
			totalPage = allMainArticCount/rows;
		}else{
			totalPage = allMainArticCount/rows+1;
		}
		String email = null;
		String pass = null;
		String ip = null;
		Cookie[] cookies = request.getCookies();
		if(cookies!=null){
			for (Cookie cookie : cookies) {
				if(cookie.getName().equals("user1")){
					email = cookie.getValue();
				}
				if(cookie.getName().equals("user2")){
					pass = cookie.getValue();
				}
			}
			if(email==null||pass==null){
				session.setAttribute("boardInfo", boardService.queryAllChild());//查询出所有父板块对应的所以子版块的信息并保存到session中
				session.setAttribute("curPage", 1);//当前页数
				session.setAttribute("totalPage", totalPage);//总共页数
				session.setAttribute("mainArticleInfo", articleService.queryAllArticle(beginIndex, rows));//需要显示的帖子数
				session.setAttribute("userInfo", null);
				response.sendRedirect("user-index.jsp");
			}else{
				if(userService.checkUserEmailAndPass(email, pass)==0){
					session.setAttribute("userInfo", userService.userLoginInfo(email, pass, ip));//设置user信息的session属性以便主界面提取信息显示
					session.setAttribute("curPage", 1);//当前页数
					session.setAttribute("totalPage", totalPage);//总共页数
					session.setAttribute("mainArticleInfo", articleService.queryAllArticle(beginIndex, rows));//需要显示的帖子数
					session.setAttribute("boardInfo", boardService.queryAllChild());//查询出所有父板块对应的所以子版块的信息并保存到session中
					response.sendRedirect("user-index.jsp");
				}
			}
		}else{
			session.setAttribute("boardInfo", boardService.queryAllChild());//查询出所有父板块对应的所以子版块的信息并保存到session中
			session.setAttribute("curPage", 1);//当前页数
			session.setAttribute("totalPage", totalPage);//总共页数
			session.setAttribute("mainArticleInfo", articleService.queryAllArticle(beginIndex, rows));//需要显示的帖子数
			session.setAttribute("userInfo", null);
			response.sendRedirect("user-index.jsp");
		}
	} 
}
